using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Configuration.Provider;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Collections.Generic;

namespace XZ.Content.Mailings
{
    public abstract class MailingProvider : ProviderBase
    {
        // Properties
        public abstract string ApplicationName { get; set; }

        // methods that work with mailings
        public abstract List<Mailing> GetMailings(DateTime toDate);
        public abstract Mailing GetMailingByID(Guid mailingID);
        public abstract bool DeleteMailing(Guid mailingID);
        public abstract bool UpdateMailing(Mailing mailing);
        public abstract Guid InsertMailing(Mailing mailing);

        /// <summary>
        /// Returns a new Mailing instance filled with the DataReader's current record data
        /// </summary>
        protected virtual Mailing GetMailingFromReader(IDataReader reader)
        {
            return GetMailingFromReader(reader, true);
        }
        protected virtual Mailing GetMailingFromReader(IDataReader reader, bool readBody)
        {
            Mailing mailing = new Mailing(
               (Guid)reader["MailingId"],
               (DateTime)reader["AddedDate"],
               reader["AddedBy"].ToString(),
               reader["Subject"].ToString(),
               null, null);

            if (readBody)
            {
                mailing.PlainTextBody = reader["PlainTextBody"].ToString();
                mailing.HtmlBody = reader["HtmlBody"].ToString();
            }

            return mailing;
        }

        /// <summary>
        /// Returns a collection of Mailing objects with the data read from the input DataReader
        /// </summary>
        protected virtual List<Mailing> GetMailingCollectionFromReader(IDataReader reader)
        {
            return GetMailingCollectionFromReader(reader, true);
        }
        protected virtual List<Mailing> GetMailingCollectionFromReader(IDataReader reader, bool readBody)
        {
            List<Mailing> mailings = new List<Mailing>();
            while (reader.Read())
                mailings.Add(GetMailingFromReader(reader, readBody));
            return mailings;
        }

        protected int ExecuteNonQuery(DbCommand cmd)
        {
            if (HttpContext.Current.User.Identity.Name.ToLower() == "sampleeditor")
            {
                foreach (DbParameter param in cmd.Parameters)
                {
                    if (param.Direction == ParameterDirection.Output ||
                       param.Direction == ParameterDirection.ReturnValue)
                    {
                        switch (param.DbType)
                        {
                            case DbType.AnsiString:
                            case DbType.AnsiStringFixedLength:
                            case DbType.String:
                            case DbType.StringFixedLength:
                            case DbType.Xml:
                                param.Value = "";
                                break;
                            case DbType.Boolean:
                                param.Value = false;
                                break;
                            case DbType.Byte:
                                param.Value = byte.MinValue;
                                break;
                            case DbType.Date:
                            case DbType.DateTime:
                                param.Value = DateTime.MinValue;
                                break;
                            case DbType.Currency:
                            case DbType.Decimal:
                                param.Value = decimal.MinValue;
                                break;
                            case DbType.Guid:
                                param.Value = Guid.Empty;
                                break;
                            case DbType.Double:
                            case DbType.Int16:
                            case DbType.Int32:
                            case DbType.Int64:
                                param.Value = 0;
                                break;
                            default:
                                param.Value = null;
                                break;
                        }
                    }
                }
                return 1;
            }
            else
                return cmd.ExecuteNonQuery();
        }

        protected IDataReader ExecuteReader(DbCommand cmd)
        {
            return ExecuteReader(cmd, CommandBehavior.Default);
        }

        protected IDataReader ExecuteReader(DbCommand cmd, CommandBehavior behavior)
        {
            return cmd.ExecuteReader(behavior);
        }

        protected object ExecuteScalar(DbCommand cmd)
        {
            return cmd.ExecuteScalar();
        }
    }
}